package uk.ac.aber.paws.client.state;

import uk.ac.aber.paws.client.constants.Constants;
import uk.ac.aber.paws.client.core.Initiator;
import uk.ac.aber.paws.client.core.PAWSClient;
import uk.ac.aber.paws.client.db.DBLogger;
import uk.ac.aber.paws.client.util.*;
import java.util.*;
import java.util.logging.Level;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 8th August 2011
 * 
 * LoginProcessOperations.java
 * 
 * This class deals with the retrieving of log in sessions from the operating system and
 * stores the relevant data in the Login object. 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class LoginProcessOperations {

	private static long lastCheck = 0l;
	
	/**
	 * Kicks off a user login lookup, if a user login is detected, value set to loginObj.
	 * @param loginObj The object storing the current service information. Needed to provide the computer name that it is running on.
	 */
	public static void processLogin() {
		
		// log this check
		lastCheck = Calendar.getInstance().getTimeInMillis();
		Initiator.LOG_FILE.log(Level.ALL,"Logins last checked at:"+lastCheck);
		
		// Start a process to find if a user is logged in, loginObj will have parameter set if so
		
		String os = PAWSClient.SYSTEM_STATE.getComputerInfo().getOperatingSystem();
		
		Vector<String> userList = null;
		
		// Windows
		if (os.equals(Constants.OS_FLAG_WINDOWS7) ||
				os.equals(Constants.OS_FLAG_WINDOWSXP) ||
				os.equals(Constants.OS_FLAG_WINDOWSVISTA)) {
			
			userList = probeUserLogins();
			
			if (userList != null) {
				//PAWSClient.SYSTEM_STATE.getLoginObj().updateLogins(userList);
				PAWSClient.SYSTEM_STATE.getLoginRegister().processLogins(userList);
			}
		}
		// Linux and Mac
		else {
			// clear out expired sessions - that haven't idle logged for set period
			PAWSClient.SYSTEM_STATE.getLoginRegister().checkForExpiredSessions();
		}
		
		Initiator.LOG_FILE.log(Level.ALL,"output login info here");
				
	}
	
	/**
	 * Run when this program initialises once only. It is invoked to check that if there is an entry in the database 
	 * for this computer with a LOGOUT time of null. If so, this is overwritten by LOGIN time + 1 hour. NULL logout 
	 * times in the database are caused by an unclean logout such as restart causing the server to think that there is 
	 * still a user logged into the computer.
	 * @param loginObj The object storing the current service information. Needed to provide the computer name that it is running on.
	 */
	public static void runPreCheck() {
		
		// if it is null, set the logout time to an hour from the login time	
		(new DBLogger()).fixLogoutForComputer(PAWSClient.SYSTEM_STATE.getComputerInfo().getComputerName());
	}

	
	/**
	 * This procedure gets a list of users logged in and validates them as genuine user logins.
	 * @param loginObj The object storing the current service information. Needed to provide the computer name that it is running on.
	 */
	private static Vector<String> probeUserLogins() {
		
		try {
			
			Vector<String> userList = LoggedInUsers.parse(true);
			Initiator.LOG_FILE.log(Level.ALL,"Logins detected:"+userList.toString());
			
			return userList;
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return null;
	}
	
	public static boolean checkLastCheck() {
		
		// check that the login check has been successfully performed in the last minute
		if (lastCheck > Calendar.getInstance().getTimeInMillis()-60000) {
		
			return true;
		}
		
		return false;
	}

}
